package com.youlin.aop;

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.ParameterizedType;

@Component
@Aspect
public class WebControllerAop {

    Logger logger = LoggerFactory.getLogger(WebControllerAop.class);

//    @Pointcut("@annotation(com.ponhu.ea2.stockcenter.provider.decorators.Idempotence)")
//    private void annotationAspect(){}

//    @Around("annotationAspect()")
    @Around("execution(* com.youlin.service.impl.Byg*ServiceImpl.*(..))")
    public Object doAroundService(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        try {
            Object[] args = proceedingJoinPoint.getArgs();
            logger.info(proceedingJoinPoint.getSignature().getName() + ": args ===>> " + JSON.toJSONString(args));
            Object object=proceedingJoinPoint.proceed();

//            MethodSignature sign = (MethodSignature) proceedingJoinPoint.getSignature();
//            ParameterizedType parameterizedType = (ParameterizedType) sign.getMethod().getGenericReturnType();
//            proceed = JSON.parseObject(redisValue, parameterizedType);

            logger.info(proceedingJoinPoint.getSignature().getName() + ": return ===>> " + JSON.toJSONString(object));
            return object;
        } catch (Throwable throwable) {
            logger.error(proceedingJoinPoint.getSignature().getName() + ": throwable ===>> ", throwable);
            throw throwable;
        }
    }

}
